<style>
    body {
        background-color: #ffffff;
    }

    .layui-iconpicker-body.layui-iconpicker-body-page .hide {
        display: none;
    }
</style>

<div class="layui-form layuimini-form">

    <input type="hidden" name="id" readonly class="layui-input" value="{$model->id}">

    <div class="layui-form-item">
        <label class="layui-form-label required">角色名称</label>
        <div class="layui-input-block">
            <input type="text" readonly class="layui-input" value="{$model->title|default=''}">
        </div>
    </div>


    <div class="layui-form-item">
        <label class="layui-form-label">权限</label>
        <div class="layui-input-block">
            <div id="node_ids" class="demo-tree-more"></div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>

</div>
<script>
    layui.use(['form', 'tree'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
        var tree = layui.tree;
        $.post('{:url("role/authorize",["id"=>$model->id])}', {}, function (res) {
            tree.render({
                elem: '#node_ids',
                data: res.data,
                showCheckbox: true,
                id: 'nodeDataId',
            });
        })

        form.on('submit(saveBtn)', function (data) {
            var checkedData = tree.getChecked('nodeDataId');
            var nodes_arr = [];
            $.each(checkedData, function (i, v) {
                nodes_arr.push(v.node);
                if (v.children !== undefined && v.children.length > 0) {
                    $.each(v.children, function (ii, vv) {
                        nodes_arr.push(vv.node);
                    });
                }
            });
            var post_data = data.field;
            if (post_data.hasOwnProperty("node")) {
                delete post_data.node;
            }
            post_data.nodes = nodes_arr;
            post_data.is_save = 1;
            postData('{:url("role/authorize")}', post_data, '授权成功');
            return false;
        });
    });
</script>